﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BestTrader.Cms.UserCenter.Models;
using Microsoft.Extensions.Logging;
using BestTrader.Cms.IServices;
using BestTrader.Cms.Services;
using BestTrader.Cms.Core.Helper;
using BestTrader.Cms.ViewModels;
using System.Security.Claims;
using FluentValidation.Results;

namespace BestTrader.Cms.UserCenter.Controllers
{
    public class ExchangeRateController : Controller
    {

        public String UserId { get { return User.Claims.FirstOrDefault(x => x.Type == "UserId")?.Value; } }
        public String RoleId { get { return User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; } }

        public String RealName
        {
            get
            {
                return User.Claims.FirstOrDefault(x => x.Type == "RealName")?.Value;
            }
        }

        private readonly IUCExchangeRateService _service;
        private readonly ICacheService _cacheService;


        public ExchangeRateController(IUCExchangeRateService service, ICacheService cacheService)
        {
            _service = service;
            _cacheService = cacheService;

        }

        public IActionResult Index() => View();



        public string LoadData([FromQuery]UCExchangeRateRequestModel model)
        {
            return JsonHelper.ObjectToJSON(_service.LoadData(model, UserId, RoleId));
        }


        [HttpGet]
        public IActionResult AddOrModify()
        {
            var result = _cacheService.GetCacheCurrencyTypes();

            ViewData["CurrencyTypes"] = result.Where(p => p.ItemCode == "USD").ToList();

            return View();
        }

        [HttpGet]
        public IActionResult Setting()
        {
            var result = _cacheService.GetCacheCurrencyTypes();

            ViewData["CurrencyTypes"] = result.Where(p => p.ItemCode == "USD").ToList();

            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        /// 
        [HttpPost, ValidateAntiForgeryToken]
        public string  AddOrModify([FromForm] UCExchangeRateModel item)
        {
            var result = new BaseResult();
            ExchangeRateValidation validationRules = new ExchangeRateValidation();
            ValidationResult results = validationRules.Validate(item);
            if (results.IsValid)
            {
                result = _service.AddOrModify(item, UserId);
            }
            else
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonModelStateInvalidCode;
                result.ResultMsg = results.ToString("||");
            }

            return JsonHelper.ObjectToJSON(result);
        }

        [HttpPost, ValidateAntiForgeryToken]
        public string Delete(int Id)
        {
            return JsonHelper.ObjectToJSON(_service.DeleteId(Id, UserId));
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
